#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" This module defines the default URL namespace.

"""
from functools import partial
from os.path import abspath, dirname, join

from tack.lib import compose
from tack.lib.authen import akoauthen, dbauthen
from tack.lib.urlns import *
from tack.lib.pdftools import pdfreport
from tack_default import api
from tack_default.api import ranks, feedback, dates, dates_weekdays, days_5, days_7


here = abspath(dirname(__file__))
static_dir = join(here, 'static')

css_dir = join(static_dir, 'css')
img_dir = join(static_dir, 'images')
js_dir = join(static_dir, 'js')
core_dir = join(static_dir, 'core')


# our URL DSL
AUTHSRC = [akoauthen, dbauthen]
COOKIE = cookie_monster
COOKIEAUTH = api.cookie_auth_app
DB = partial(confdbcon, dsn_lookup=api.default_dsn)
FILES = fileserver
FORM = formreader
FORMCOOKIE = compose([FORM, COOKIE])
FORMCOOKIEDB = compose([FORM, COOKIE, DB])
FORMDB = compose([FORM, DB])
READROLES = api.get_login_roles_app
NEEDROLES = api.require_roles_app
ROLES = compose([api.get_login_roles_app, api.require_roles_app])
SETROLES = api.set_login_roles_app


urls = [
    ('/api/login/', 'PUT|POST', FORMCOOKIEDB(api.set_login_cookies_app(AUTHSRC))),
    ('/api/logout/', 'GET|PUT|POST', COOKIE(api.delete_login_cookies)),
    ('/api/authcookies/', 'GET', 
     DB(COOKIE(COOKIEAUTH(ROLES(api.get_login_cookies, ['Modify Drivers']))))),

    ('/api/roles/', 'GET', DB(COOKIE(COOKIEAUTH(api.get_login_roles)))),
    ('/api/modules/', 'GET',  api.mod_list),
    ('/api/urls/', 'GET',  api.dispatcher_urls),
    ('/api/echo/', 'GET',  echo_app()),

    ('/api/feedback/', 'GET', DB(feedback.fetch)),
    ('/api/feedback/(?P<key>\d+)/', 'GET', DB(feedback.fetch_key)),
    ('/api/feedback/', 'PUT', FORMDB(feedback.add_entry)),
    ('/api/feedback/(?P<key>\d+)/', 'DELETE', DB(feedback.delete_entry)),

    ('/api/ranks/', 'GET', DB(ranks.get)),

    ('/api/dates/', 'GET', DB(dates.today)),
    ('/api/dates/(\d{4}\-[0-1][0-9]\-[0-3][0-9])/', 'GET', DB(dates.anyday)),
    ('/api/dates/day/', 'GET', DB(dates.today)),
    ('/api/dates/day/(\d{4}\-[0-1][0-9]\-[0-3][0-9])/', 'GET', DB(dates.anyday)),
    ('/api/dates/week/(\d{4}\-[0-1][0-9]\-[0-3][0-9])/', 'GET', DB(dates.week_span)),
    ('/api/dates/month/', 'GET', DB(dates.month)),
    ('/api/dates/month/(?P<month>.*)/', 'GET', DB(dates.anymonth)),
    
    ('/api/dates/weekdays/', 'GET', DB(dates_weekdays.today)),
    ('/api/dates/weekdays/(?P<date>.*)/', 'GET', DB(dates_weekdays.anyday)),
    
    ('/api/dates/5day/', 'GET', DB(days_5.today)),
    ('/api/dates/5day/(?P<date>.*)/', 'GET', DB(days_5.anyday)),
    
    ('/api/dates/7day/', 'GET', DB(days_7.today)),
    ('/api/dates/7day/(?P<date>.*)/', 'GET', DB(days_7.anyday)),

    ('/api/pdf/', 'POST', FORM(pdfreport(None))),

    ('/css/(?P<fname>.*)',  'GET',  FILES(css_dir, basekey='fname')),
    ('/images/(?P<fname>.*)',  'GET',  FILES(img_dir, basekey='fname')),
    ('/js/(?P<fname>.*)',  'GET',  FILES(js_dir, basekey='fname')), 
    ('/core/(?P<fname>.*)',  'GET',  FILES(core_dir, basekey='fname')),    

]

